package com.AEWMS.Service;

import com.AEWMS.Dao.Parent;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class ParentService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public ParentService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("StudentService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有父母
     * @return 所有父母信息
     */
    public ArrayList<Parent> findAllPa(){
        ArrayList<Parent> allPaList = new ArrayList();//升级版数组，返回所有学生对象
        String sql = "SELECT * FROM parent_info";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                Parent s = new Parent();//按行查找，为false时查询结束
                s.setParentId(rs.getInt(1));
                s.setParentName(rs.getString(2));
                s.setParentPassword(rs.getString(3));
                s.setParentRealName(rs.getString(4));
                s.setParentSex(rs.getString(5));
                s.setParentTel(rs.getString(6));
                s.setStudentNumber(rs.getString(7));
                s.setType(rs.getString(8));
                allPaList.add(s);//将查找到的行（每个父母对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllPa 失败");
            System.out.println(e);
        }
        return allPaList;//输出包含所有父母对象的数组
    }

    /**
     * 查找父母
     * @param id 通过tel
     * @return 返回一个父母对象
     */
    public Parent findPaById(String id){
        Parent s = new Parent();
        String sql = "SELECT * FROM parent_info WHERE parent_id = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setParentId(rs.getInt(1));
                s.setParentName(rs.getString(2));
                s.setParentPassword(rs.getString(3));
                s.setParentRealName(rs.getString(4));
                s.setParentSex(rs.getString(5));
                s.setParentTel(rs.getString(6));
                s.setStudentNumber(rs.getString(7));
                s.setType(rs.getString(8));
            }
        }
        catch (Exception e) {
            System.out.println("findPaById 失败");
            System.out.println(e);
        }
        return s;
    }
    /**
     * 查找孩子
     * @param id 通过学号
     * @return 返回一个父母对象
     */
    public Parent findChildById(String id){
        Parent s = new Parent();
        String sql = "SELECT * FROM parent_info WHERE student_number = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setParentId(rs.getInt(1));
                s.setParentName(rs.getString(2));
                s.setParentPassword(rs.getString(3));
                s.setParentRealName(rs.getString(4));
                s.setParentSex(rs.getString(5));
                s.setParentTel(rs.getString(6));
                s.setStudentNumber(rs.getString(7));
                s.setType(rs.getString(8));
            }
        }
        catch (Exception e) {
            System.out.println("findPaById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 添加父母
     * @return 一个父母对象
     */
    public boolean insertPa(Parent s){
        boolean flag = false;
        String sql = "INSERT INTO parent_info(parent_name, parent_password, parent_realname, parent_sex,parent_tel, student_number,type) VALUES (?, ?, ?, ?, ?, ?,'parent')";
        try {
            this.pstmt = conn.prepareStatement(sql);
            //sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setString(1,s.getParentName());
            this.pstmt.setString(2,s.getParentPassword());
            this.pstmt.setString(3,s.getParentRealName());
            this.pstmt.setString(4,s.getParentSex());
            this.pstmt.setString(5,s.getParentTel());
            this.pstmt.setString(6,s.getStudentNumber());
            int rs = this.pstmt.executeUpdate();
            //这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertPa defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个父母
     * @param id,password
     * @return 是否成功
     */
    public boolean updatePa(String id,String password) {
        boolean flag = false;
        String sql = "UPDATE parent_info SET  parent_password = ? WHERE parent_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,password);
            this.pstmt.setString(2,id);
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updatePa 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个父母
     * @param id 父母tel
     * @return 是否成功
     */
    public boolean deletePaById(String id){
        boolean flag = false;
        String sql = "DELETE FROM parent_info WHERE parent_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);//这里的名字与dao.admin传进来的一致
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deletePaById 失败");
            System.out.println(e);
        }
        return flag;
    }

}

